﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace shopform
{
    public class CompanyReferent
    {
        public String CompanyReferentName;
        public String CompanyReferentMail;
        public String CompanyReferentPhoneNr;
        public String CompanyID;

        /// <summary>
        /// Simple empty constructor.
        /// </summary>
        public CompanyReferent()
        {
        }

        /// <summary>
        /// Constructor for a single given CompanyReferent.
        /// </summary>
        /// <param name="CompRefID">ReferentID (primary key)</param>
        public CompanyReferent(string CompRefID)
        {
            try
            {
                String q = "SELECT * FROM CompanyReferents " +
                           "WHERE CompanyReferentMail = '" + CompRefID + "'";

                DataTable dt = myDatabaseCommander.ExecuteArray(q);
                foreach (DataRow r in dt.Rows)
                {
                    this.CompanyReferentName = r["CompanyReferentName"].ToString();
                    this.CompanyReferentMail = r["CompanyReferentMail"].ToString();
                    this.CompanyReferentPhoneNr = r["CompanyReferentPhoneNr"].ToString();
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Get all referents for a company.
        /// </summary>
        /// <param name="CompID">Company ID</param>
        /// <returns>Collection of Company Referents belonging to a company.</returns>
        public static DataTable getAll(string CompID)
        {
            try
            {
                String q = "SELECT * FROM CompanyReferents " +
                           "WHERE CompanyID = '" + CompID + "';";
                return myDatabaseCommander.ExecuteArray(q);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Add new company referent.
        /// </summary>
        public void insertCompanyReferent()
        {
            try
            {
                String query;
                query = "INSERT INTO CompanyReferents(CompanyReferentName, CompanyReferentMail, " +
                        "CompanyReferentPhoneNr, CompanyID) " +
                        "VALUES ('" + this.CompanyReferentName +
                                "', '" + this.CompanyReferentMail +
                                "', '" + this.CompanyReferentPhoneNr +
                                "', '" + this.CompanyID + "');";
                myDatabaseCommander.ExecuteNonQuery(query);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Update information for a comp referent.
        /// </summary>
        public void updateCompanyReferent()
        {
            try
            {
                String q = "UPDATE CompanyReferents SET " + 
                           "CompanyReferentName = '" + this.CompanyReferentName + "', " +
                           "CompanyReferentPhoneNr = '" + this.CompanyReferentPhoneNr + "' " +
                           "WHERE CompanyReferentMail = '" + this.CompanyReferentMail + "';";

                myDatabaseCommander.ExecuteNonQuery(q);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Delete a company referent.
        /// </summary>
        public void deleteCompanyReferent()
        {
            try
            {
                String q =
                    "DELETE FROM CompanyReferents " +
                    "WHERE CompanyReferentMail = '" + this.CompanyReferentMail + "'";

                myDatabaseCommander.ExecuteNonQuery(q);
            }
            catch (Exception e)
            {
                throw e;
            }
        }


    }
}
